<?php

/***  DOCUMENTATION LAYER

Ceo Sample Unit Test

Last Update: Aug 2007
  
DESCRIPTION
  this file provides a template for other simple unit tests.
  
NOTES
  $test_root dynamically sets root in order to keep tests working after
  they are moved to the __REPOSITORY directory
    
______________________________________________________________________________*/

$_tx0 = microtime();
$test_root = ( strpos(dirname(__FILE__), 'repository') === false ) ? '' : '../';


// Test Name
$test_name = 'sample test';

// Include Files
require_once($test_root . '../php_ceo/framework.class.php');

// Configure Test
$debug = 1;


// compare php and js md5
if ( $_POST['js_md5'] )
{
  $salt = $_POST['salt'];
  $jmd5 = $_POST['js_md5'];
  $pmd5 = md5($_POST['plaintext'] . $salt);
  $cf = ( $jmd5 == $pmd5 ) ? 'matched' : 'not matched';

  
$compare = <<<HTML

<h2>comparison results</h2>
<pre>js : $jmd5</pre>
<pre>php: $pmd5</pre>
<p>php compare: $cf</p>
<pre>plaintext: {$_POST['plaintext']}</pre>
<pre>salt: $salt</pre>

HTML;
}

// set salt
$salt = md5(mt_rand(1,100000));

// form html
$head = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>md5 test</title>
<script type="text/javascript" src="http://klenwell.googlecode.com/svn/trunk/JS/kwyption/md5.js"></script>
<script type="text/javascript">

function cx_hidden_input_field(name, value)
{
  var Obj = document.createElement("input");
  Obj.setAttribute("type", "hidden");
  Obj.setAttribute("name", name);
  Obj.setAttribute("value", value);
  return Obj;
}

function run_md5() {

  // get values submitted
  var LoginForm = document.getElementById("login_form");
  var plaintext1 = LoginForm.plaintext0.value;
  var salt1 = LoginForm.salt0.value;
  var md51 = hexMD5(plaintext1 + salt1);

  // auto submit data
  var AutoForm = document.getElementById("submit_form");
  
  // add js_data as hidden field
  var InputText = cx_hidden_input_field('plaintext', plaintext1);
  var InputSalt = cx_hidden_input_field('salt', salt1);
  var InputMd5  = cx_hidden_input_field('js_md5', md51);
  document.forms['submit_form'].appendChild(InputText);
  document.forms['submit_form'].appendChild(InputSalt);
  document.forms['submit_form'].appendChild(InputMd5);
  
  // Call the form's submit method
  AutoForm.submit();
}

</script>

</head>

HTML;


$form = <<<HTML

<h2>Click here to compare php md5 with js</h2>
<form id="login_form" action="#" method="post">
<input type="text" name="plaintext0" value="plaintext (you can change this)" />
<input type="hidden" name="salt0" value="{$salt}" />
<input type="button" name="submit" value="test" onclick="javascript:run_md5();" />
</form>

<form id="submit_form" action="#" method="post">
<input type="hidden" name="salt0" value="{$salt}" />
</form>

    
HTML;


// Footer
$_txn = microtime();
$stx = number_format(((substr($_txn,0,9)) + (substr($_txn,-10)) - (substr($_tx0,0,9)) - (substr($_tx0,-10))),4); 
$footer = "<h6>test '$test_name' complete in $stx s</h6>";


// output
$html = <<<HTML
$head
<body>
$compare
$form 
$footer
</body>
</html>
HTML;

print $html;

?>
